package com.nf.bookstytem.uitl;

import java.io.IOException;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.Properties;

import javax.sql.DataSource;

import com.alibaba.druid.pool.DruidDataSource;
import com.alibaba.druid.pool.DruidDataSourceFactory;

public class DataSourceHelper {
private static DruidDataSource druidDataSource=null;
	static {
		Properties properties=new Properties();
		try {
			properties.load(DataSourceHelper.class.getResourceAsStream("/dbconfig.properties"));
			druidDataSource=(DruidDataSource) DruidDataSourceFactory.createDataSource(properties);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}catch(Exception e) {
			e.printStackTrace();
		}
	}
	public static DataSource getDataSource() {
		return druidDataSource;
	}
	public static Connection getConnection() {
		try {
			return druidDataSource.getConnection();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			throw new RuntimeException("获取连接失败");
		}
	}
	
	public static void main(String[] args) {
		System.out.println(DataSourceHelper.getConnection());
	}
}
